/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary;

import entity.Event;
import entity.Event_;
import entity.User;
import entity.User_;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Root;

/**
 *
 * @author andrea
 */

@Stateless
public class EventManager {
    
    @PersistenceContext
    private EntityManager em;
  
    /**
     *
     * @param id
     * @return
     */
    public Event loadSpecificevent(long id){
      return em.find(Event.class, id);
    }
    
    
    public List<Event> findEventForSpecificUser(String mail){
        CriteriaBuilder cb= em.getCriteriaBuilder();
        CriteriaQuery<Event> cq = cb.createQuery(Event.class);
        
        Root<Event> eventRoot = cq.from(Event.class);
        Join<Event,User> join = eventRoot.join(Event_.participants);
  
        cq.select(eventRoot);
        cq.where(cb.equal(join.get(User_.email), mail));
                
        return em.createQuery(cq).getResultList();
    }
    
    public List<Event> findPublicEvent(){
        CriteriaBuilder cb= em.getCriteriaBuilder();
        CriteriaQuery cq = cb.createQuery();
        
        Root root = cq.from(Event.class);
        
        cq.select(cq.from(Event.class));
        cq.where(cb.equal(root.get(Event_.publicEvent), true));

                
        return em.createQuery(cq).getResultList();    
    }
    
    /**
     *
     * @param id
     * @return
     */
    public Event findSpecificEvent(Long id){
        CriteriaBuilder cb= em.getCriteriaBuilder();
        CriteriaQuery<Event> cq = cb.createQuery(Event.class);
        
        Root<Event> root = cq.from(Event.class);
        
        cq.select(cq.from(Event.class));
        cq.where(cb.equal(root.get(Event_.eventID), id));

                
        return (Event) em.createQuery(cq).getSingleResult();
    }
    
    /**
     *
     * @param event
     * @param owner
     */
    public void createEvent(Event event, User owner){      
      //set the owner
      event.setOwner(owner);
      //set the owner as participant
      event.getParticipants().put(owner.getEmail(),owner);
      //set the event not cancelled
      event.setCancelled(false);
      //add the entity in the DB
      em.persist(event);
    }
    
    /**
     *
     * @param event
     */
    public void modifyEvent(Event event){
        //refresh the event in the DB
        em.merge(event);
    }

    /**
     *
     * @param event
     */
    public void deleteEvent(Event event){
      //remove the entity in the DB
      event.setCancelled(true);
      em.refresh(event);
    }
    
    /**
     *
     * @param event
     * @param participantToRemove
     */
    public void removeParticipant(Event event,User participantToRemove){
      event.getParticipants().remove(participantToRemove.getEmail());
      
      em.persist(event);
    }
    
    /**
     *
     * @param event
     * @param participants
     */
    public void addParticipants(Event event,List<User> participants){
        for(User x: participants){
            event.getParticipants().put(x.getEmail(),x);
        }        
        em.persist(event);
    }
    
    public void addPArticiPant(Event event , User newParticipant){
        event.addPArticipant(newParticipant);
        em.merge(event);
    }
    
    /**
     *
     * @param user
     * @param event
     * @return
     */
    public boolean isTheOwner(User user,Event event){
       return user.equals(event.getOwner());
    }

    
    
}
